﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ImbedLibrary
{
    public static class MyObject
    {

        /// <summary>
        /// 返回sql语句的数据字段
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetData(this object obj)
        {
            if (obj == null)
            {
                return " is Null";
            }
            switch (obj.GetType().Name)
            {
                case "Int32":
                case "UINT32":
                case "Boolean":
                    return "= " + obj.ToString();
                case "DateTime":
                    return "= #" + Convert.ToDateTime(obj).ToString2() + "#";
                case "String":
                    return "= '" + obj.ToString().Replace("'", "''") + "'";
                default:
                    return "= '" + obj.ToString() + "' ";
            }
        }

        /// <summary>
        /// 转换数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static Int32 ToInt32(this object obj)
        {
            try
            {
                return Convert.ToInt32(obj);
            }
            catch (Exception)
            {
                return 0;
            }
        }
            
    }
}
